﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class TimKiem : System.Web.UI.Page
{
    string sql;
    DataTable dtb;
    protected void Page_Load(object sender, EventArgs e)
    {
        string tenxe;
        string hangxe;
        string model;
        string tinhtrang;
        string namSX;
        //if (Request.QueryString["TenXe"] == "")
        //{
        //    tenxe = "ANY";
        //}
        //else
        //{
            tenxe = Request.QueryString["TenXe"];
        //}
        if (Request.QueryString["HangXe"] == "Tất cả")
        {
            hangxe = "";
        }
        else
        {
            hangxe = Request.QueryString["HangXe"];
        }
        if (Request.QueryString["Model"] == "Tất cả")
        {
            model = "";
        }
        else
        {
            model = Request.QueryString["Model"];
        }
        if (Request.QueryString["TinhTrang"] == "Tất cả")
        {
            tinhtrang = "";
        }
        else
        {
            tinhtrang = Request.QueryString["TinhTrang"];
        }
        if (Request.QueryString["NamSanXuat"] == "Tất cả")
        {
            namSX = "";
        }
        else
        {
            namSX = Request.QueryString["NamSanXuat"];
        }
        sql = string.Format(@"SELECT Distinct ttcb.MaXe,ttcb.TenXe,ttcb.NgoaiThat,ttcb.NamSX,ttcb.Image
                                     FROM ThongTinCoban ttcb,hangxe hx,model md
                                     WHERE ttcb.TenXe like '%{0}%'
                                        AND hx.TenHang like '%{1}%'
                                        AND md.Model like '%{2}%'
                                        AND ttcb.TinhTrang like '%{3}%'
                                        AND ttcb.NamSX like '%{4}%'
                                        AND ttcb.MaHangXe=hx.MaHangXe AND hx.MaHangXe=md.MaHangXe
                                    ", tenxe, hangxe, model, tinhtrang, namSX);        

        if (!IsPostBack)
        {
            this.Display();
        }
    }
    protected void Display()
    {
        dtb = DataBase.GetDataTable(sql);
        if (dtb.Rows.Count == 0)
        {
            Label1.Text = "Không tìm thấy dữ liệu nào phù hợp!";
        }
        else
        {
            Label1.Text = "";
            DataList1.DataSource = dtb;
            DataList1.DataBind();
            PhanTrang();
        }
    }
    protected void ImageButton_Click(object sender, ImageClickEventArgs e)
    {
        ImageButton ibtn = (ImageButton)sender;
        if (IsPostBack)
        {
            Response.Redirect("~/ProductDetail.aspx?Product=" + ibtn.CommandArgument);

        }
    }
    void PhanTrang()
    {

        CollectionPager1.DataSource = dtb.DefaultView;
        CollectionPager1.PageSize = 12;
        CollectionPager1.BindToControl = DataList1;
        DataList1.DataSource = CollectionPager1.DataSourcePaged;

    }
    protected void btnCartAdd_Click(object sender, ImageClickEventArgs e)
    {
        ShoppingCart cart = (ShoppingCart)Session["ShoppingCart"];

        ImageButton btn = (ImageButton)sender;
        string maxe = btn.CommandArgument;
        cart.Add(maxe, 1);

        Response.Redirect("ViewCart.aspx");
    }
}
